<?php
require '../home/config.php';
ob_clean();
$action = getValue('action','str','POST','');
$res = array();
switch($action) {
    case 'add_comment' :
        $comment_news_id = getValue('comment_news_id','int','POST',0);
        $comment_name = getValue('comment_name','str','POST','');
        $comment_email = getValue('comment_email','str','POST','');
        $comment_title = getValue('comment_title','str','POST','');
        $comment_content = getValue('comment_content','str','POST','');
        $comment_security = getValue('comment_security','str','POST','');
        if($comment_security == $_SESSION['securitycode']) {
            $db_insert_comment = new db_execute('INSERT INTO comments(
                                                            com_news_id,
                                                            com_name,
                                                            com_email,
                                                            com_title,
                                                            com_content,
                                                            com_date,
                                                            com_like,
                                                            com_dislike,
                                                            com_active)
                                                        VALUES(
                                                            '.$comment_news_id.',
                                                            "'.$comment_name.'",
                                                            "'.$comment_email.'",
                                                            "'.$comment_title.'",
                                                            "'.$comment_content.'",
                                                            '.time().',
                                                            0,0,1)');
            if($db_insert_comment->total == 1) {
                $res['succsess'] = 1;
                $res['notify'] = 'Gửi bình luận thành công !';
            }else {
                $res['succsess'] = 2;
                $res['notify'] = 'Gửi bình luận thất bại, có lỗi xảy ra !';
            }
        }else {
            $res['succsess'] = 2;
            $res['notify'] = 'Gửi bình luận thất bại, mã xác nhận không đúng !';
        }
        echo json_encode($res);
    break;
    
    case 'comments_like' :
        $id = getValue('id','int','POST',0);
        $remote = getValue('remote','str','POST','');
        $_SESSION[$remote] = $remote;
        $db_update_like = new db_execute('UPDATE comments SET com_like = com_like + 1 WHERE com_id = '.$id);
        $res['succsess'] = 1;
        echo json_encode($res);
    break;
    
    case 'comments_dislike' :
        $id = getValue('id','int','POST',0);
        $remote = getValue('remote','str','POST','');
        $_SESSION[$remote] = $remote;
        $db_update_like = new db_execute('UPDATE comments SET com_dislike = com_dislike + 1 WHERE com_id = '.$id);
        $res['succsess'] = 1;
        echo json_encode($res);
    break;
    
    case 'next_comment' :
        $id = getValue('id','int','POST',0);
        $page = getValue('page','int','POST',0);
        $start = ($page - 1)*5;
        $db_next = new db_query('SELECT * FROM comments WHERE com_news_id = '.$id.' AND com_active = 1 LIMIT '.$start.',5');
        $next = $db_next->resultArray();unset($db_next);
        if($next) {
            $res['html'] = '<ul>';
            foreach($next as $comment) {
                //Session like và dislike
                if(isset($_SESSION[$_SERVER['REMOTE_ADDR'].$comment['com_id']])) {
                    $lsec = 1;
                }else {
                    $lsec = 2;
                }
                $res['html'] .= '<li data-like="'.$lsec.'" data-remote="'.$_SERVER['REMOTE_ADDR'].$comment['com_id'].'" data-id="'.$comment['com_id'].'">
                    <h4><span>'.$comment['com_name'].':</span> '.$comment['com_title'].'</h4>
                    <h5>'.$comment['com_content'].'</h5>
                    <p>
                        <span class="comments_dislike" onclick="comments_dislike(this);"><i class="icon-thumbs-down"></i> '.$comment['com_dislike'].'</span>
                        <span class="comments_like" onclick="comments_like(this);"><i class="icon-thumbs-up"></i> '.$comment['com_like'].'</span>
                        <span class="comments_time">Gửi lúc '.date('H:i - d/m/Y',$comment['com_date']).'</span>
                    </p>
                </li>';
            }
            $res['html'] .= '</ul>';
            if(count($next) > 5) {
                $res['add'] = 1;
            }else {
                $res['add'] = 2;
            }
        }else {
            $res['html'] = '';
            $res['add'] = 2;
        }
        echo json_encode($res);
    break;
    
}

?>
